<template>
  <!-- 表格位置 表格组件 -->
  <el-table :data="tableData.list" style="width: 100%" border>
    <el-table-column prop="paper_name" label="试卷名称" width="150">
    </el-table-column>
    <el-table-column prop="subject_name" label="学科名称" width="150">
    </el-table-column>
    <el-table-column prop="directory_name" label="目录名称"> </el-table-column>
    <el-table-column label="试卷类型">
      <template slot-scope="scope" width="120">
        {{ scope.row.paper_type === 2 ? "真题卷" : "模拟卷" }}
      </template>
    </el-table-column>
    <el-table-column label="试卷状态">
      <template slot-scope="scope">
        <!-- value 是单向绑定用于switch -->
        <el-switch
          v-model="scope.row.show_state"
          :active-value="1"
          :inactive-value="2"
          @change="changeFn(scope.row.id, scope.row.show_state)"
        ></el-switch>
      </template>
    </el-table-column>
    <el-table-column label="分数情况">
      <template slot-scope="scope">
        {{ scope.row.pass_score }} /{{ scope.row.total_score }}
      </template>
    </el-table-column>
    <el-table-column label="试卷难度">
      <template slot-scope="scope">
        {{
          scope.row.difficulty === 1
            ? "简单"
            : scope.row.difficulty === 2
            ? "中等"
            : "较难"
        }}
      </template>
    </el-table-column>
    <el-table-column prop="create_name" label="创建人"> </el-table-column>
    <el-table-column label="创建时间">
      <template slot-scope="scope">
        {{ new Date(scope.row.created_at).toLocaleDateString() }}
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-tooltip effect="dark" content="查看" placement="top">
          <el-button
            style="margin: 0"
            icon="el-icon-view"
            circle
            @click="handleClick(scope.row.id)"
            type="text"
            size="small"
          ></el-button>
        </el-tooltip>
        <el-tooltip effect="dark" content="修改" placement="top">
          <el-button
            style="margin: 0; padding: 0"
            icon="el-icon-edit"
            circle
            @click="editFn(scope.row.id)"
            type="text"
            size="small"
          ></el-button
        ></el-tooltip>
        <el-tooltip effect="dark" content="删除" placement="top">
          <el-button
            style="margin: 0"
            icon="el-icon-delete"
            circle
            type="text"
            @click="delFn(scope.row.show_state, scope.row.id)"
            size="small"
          ></el-button
        ></el-tooltip>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
import { changeStateApi, delPaperApi } from "@/api/paperInfo";
export default {
  name: "tablePaper",
  props: {
    tableData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {};
  },
  methods: {
    async changeFn(paper_id, paper_state) {
      try {
        await changeStateApi(paper_id, paper_state);
      } catch (error) {
        return console.log(error);
      }
    },
    async delFn(show_state, id) {
      // 删除
      const idx = this.tableData.list.findIndex((item) => item.id === id);
      if (show_state === 1)
        return this.$message.warning("禁用情况下才可以删除");
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          try {
            this.tableData.list.splice(idx, 1);
            delPaperApi(id);
          } catch (error) {
            return console.log(error);
          }
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      // 通知父组件跟新数据
    },
    // 查看
    async handleClick(id) {
      this.$emit("lookState", id);
    },
    // 修改
    editFn(id) {
      this.$emit("editState", id);
    },
  },
};
</script>